<?php


namespace tools\lib\extend\admin;

use tools\lib\main\Common;

/**
 * 后台控制器
 * Class Admin
 * @package app\common\command\create\file
 */
class Admin extends Common
{
    /**
     * @var string
     */
    protected $suffix = '';

    public function template()
    {

        $template = <<<EOT
<?php

namespace app\admin\controller;

class {$this->className()} extends Basic
{
    /**
     * 表头数据
     * @return array
     */
    protected function header()
    {
        return {$this->header()};
    }

    /**
     * 表单数据
     * @return array
     */
    protected function form()
    {
        return {$this->form()};
    }
}
EOT;
        return $template;
    }

    /**
     * @param null|callable $callback
     * @return string
     */
    private function transFields($callback = null)
    {
        $fields = '';

        $columns = tableColumns($this->parameter->getName());

        if ($columns) {
            foreach ($columns as $column) {

                $result = $callback($column['Field']);

                if ($result === false) {
                    continue;
                }

                if (!preg_match('/^\[/', $result)) {
                    $result = '            \''
                        . $result
                        . '\','
                        . "\n";
                } else {
                    $result = '            ' . $result . ',' . "\n";
                }

                $fields .= $result;
            }
        }

        $fields = '['
            . "\n"
            . $fields
            . '        ]';

        return $fields;
    }

    /**
     * 转化头部信息
     * @return string
     */
    private function header()
    {
        return $this->transFields(function ($data) {

            if (empty($data)) {
                return false;
            }

            if (in_array($data, ['update_time', 'delete_time'])) {
                return false;
            }

            switch ($data) {
                case 'status':
                    $data = "['field' => 'status', 'type' => 'switch', 'width' => 100, 'option' => '关闭|开启']";
                    break;
                case 'type':
                    $data = $data . '_text';
                    break;
                case 'sort':
                    $data = "['field' => 'sort', 'edit' => 'text']";
                    break;
            }

            return $data;
        });
    }

    /**
     * 设置表单数据
     * @return string
     */
    private function form()
    {
        return $this->transFields(function ($data) {

            if (empty($data)) {
                return false;
            }

            if (in_array($data, ['id', 'create_time', 'update_time', 'delete_time'])) {
                return false;
            }

            switch ($data) {
                case 'status':
                    $data = "['field' => '$data', 'type' => 'radio', 'option' => {$this->className()}Logic::instance()->transStatus()]";
                    break;
                case 'image':
                case 'img':
                case 'images':
                case 'photo':
                case 'pic':
                case 'pics':
                case 'picture':
                case 'pictures':
                    $data = "['field' => '$data', 'type' => 'file', 'size' => 1024]";
                    break;
                case 'sort':
                    $data = "['field' => '$data', 'required' => false, 'value' => 99]";
                    break;
                case 'content':
                    $data = "['field' => '$data', 'type' => 'editor']";
                    break;
                case 'province_id':
                case 'province':
                case 'city_id':
                case 'city':
                case 'area_id':
                case 'area':
                case 'region':
                    $data = "['field' => '$data', 'type' => 'region']";
                    break;
                case 'start_time':
                case 'end_time':
                    $data = "['field' => '$data', 'type' => 'datetime']";
                    break;
            }
            return $data;
        });
    }
}